local fengming = fk.CreateSkill {
  name = "lingling__fengming",
  tags = { Skill.Switch }
}

Fk:loadTranslationTable{
  ["lingling__fengming"] = "凤鸣",
  [":lingling__fengming"] = "转换技，当你交替使用不同颜色的牌时，你可以①摸一张牌②重铸一张牌。",

  ["@lingling__fengming"] = "凤鸣",
  ["#lingling__fengming-yang"] = "凤鸣：是否摸一张牌？",
  ["#lingling__fengming-yin"] = "凤鸣：是否重铸一张牌？",
}

fengming:addEffect(fk.CardUsing, {
  anim_type = "drawcard",
  can_trigger = function(self, event, target, player, data)
    if target == player and player:hasSkill(fengming.name) then
      local use_events = player.room.logic:getEventsByRule(GameEvent.UseCard, 2, function (e)
        return e.data.from == player
      end, nil, Player.HistoryGame)
      if #use_events == 2 then
        local color = use_events[2].data.card.color
        if color == Card.NoColor then
          player.room:setPlayerMark(player, "@lingling__fengming", 0)
        else
          player.room:setPlayerMark(player, "@lingling__fengming", data.card:getColorString())
          if data.card.color ~= Card.NoColor and color ~= data.card.color then
            if player:getSwitchSkillState(fengming.name, false) == fk.SwitchYang then
              return true
            else
              return not player:isNude()
            end
          end
        end
      end
    end
  end,
  on_cost = function (self, event, target, player, data)
    local room = player.room
    if player:getSwitchSkillState(fengming.name, false) == fk.SwitchYang then
      return room:askToSkillInvoke(player, {
        skill_name = fengming.name,
        prompt = "#lingling__fengming-yang",
      })
    else
      local cards = room:askToCards(player, {
        min_num = 1,
        max_num = 1,
        include_equip = false,
        skill_name = fengming.name,
        prompt = "#lingling__fengming-yin",
        cancelable = true,
      })
      if #cards > 0 then
        event:setCostData(self, {cards = cards})
        return true
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    if player:getSwitchSkillState(fengming.name, true) == fk.SwitchYang then
      player:drawCards(1, fengming.name)
    else
      player.room:recastCard(event:getCostData(self).cards, player, fengming.name)
    end
  end,

  can_refresh = function(self, event, target, player, data)
    return target == player and player:hasSkill(fengming.name, true)
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    if data.card.color == Card.NoColor then
      room:setPlayerMark(player, "@lingling__fengming", 0)
    else
      room:setPlayerMark(player, "@lingling__fengming", data.card:getColorString())
    end
  end,
})

fengming:addLoseEffect(function (self, player, is_death)
  player.room:setPlayerMark(player, "@lingling__fengming", 0)
end)

return fengming
